// 引入path包
const path = require('path');

// 引入HTML包
const HTMLWebpackPlugin = require('html-webpack-plugin');

// 清除上一次导出的包
const {CleanWebpackPlugin} = require('clean-webpack-plugin');

module.exports = {

    mode: 'development',

    // webpack的入口文件
    entry: '/src/index.ts',

    // webpack的出口文件
    output: {
        // 文件打包后的输出路径
        path: path.resolve(__dirname, 'dist'),
        // 文件打包后的文件名
        filename: 'bundle.js',
        environment: {
            arrowFunction: false,
            const: false
        }
    },

    devServer: {
        port: 8080,
        open: true,
    },

    module: {
        rules: [
            {
                test: /\.ts$/,
                use: [
                    {
                      loader: 'babel-loader',
                      options: {
                          presets: [
                              [
                                  '@babel/preset-env',
                                  {
                                      targets: {
                                          "ie": "11",
                                          "chrome": "58"
                                      },
                                      "corejs": "3",
                                      "useBuiltIns": "usage"
                                  }
                              ]
                          ]
                      }
                    },
                    'ts-loader'
                ],
                exclude: /node_modules/
            },
            {
                test: /\.less/,
                use: [
                    'style-loader',
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: {
                                plugins: [
                                    [
                                        'postcss-preset-env',
                                        {
                                            browsers: "last 2 versions"
                                        }
                                    ]
                                ]
                            }
                        }
                    },
                    'less-loader'
                ]
            }
        ]
    },

    resolve: {
        extensions: ['.ts', '.js'],
        alias: {
            "@": path.resolve(__dirname, 'src'),
            "@config": path.resolve(__dirname, 'src/config'),
            "@modules": path.resolve(__dirname, "src/modules")
        }
    },

    plugins: [
        new CleanWebpackPlugin(),
        new HTMLWebpackPlugin({
            template: "./index.html"
        })
    ]
}

console.log(__dirname);
